﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl x:Class="Microsoft.Windows.Controls.Samples.DataGridAutoCompleteBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
    <StackPanel>
        <StackPanel.Resources>
            <Style x:Key="DataGridAutoCompleteTemplate" TargetType="controls:AutoCompleteBox">
                <Setter Property="Background" Value="#FF1F3B53"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="BorderBrush">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFA3AEB9" Offset="0"/>
                            <GradientStop Color="#FF8399A9" Offset="0.375"/>
                            <GradientStop Color="#FF718597" Offset="0.375"/>
                            <GradientStop Color="#FF617584" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:AutoCompleteBox">
                            <Grid Margin="{TemplateBinding Padding}">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="PopupStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition GeneratedDuration="0:0:0.2" To="PopupOpened" />
                                            <VisualTransition GeneratedDuration="0:0:0.5" To="PopupClosed" />
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="PopupOpened">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="1.0" />
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="PopupClosed">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="0.0" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <TextBox IsTabStop="True" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" Margin="0" />
                                <Popup x:Name="Popup">
                                    <Border x:Name="PopupBorder" HorizontalAlignment="Stretch" Opacity="0.0" BorderThickness="0" CornerRadius="3">
                                        <Border.RenderTransform>
                                            <TranslateTransform X="2" Y="2" />
                                        </Border.RenderTransform>
                                        <Border.Background>
                                            <SolidColorBrush Color="#11000000" />
                                        </Border.Background>
                                        <Border HorizontalAlignment="Stretch" BorderThickness="0" CornerRadius="3">
                                            <Border.Background>
                                                <SolidColorBrush Color="#11000000" />
                                            </Border.Background>
                                            <Border.RenderTransform>
                                                <TransformGroup>
                                                    <ScaleTransform />
                                                    <SkewTransform />
                                                    <RotateTransform />
                                                    <TranslateTransform X="-1" Y="-1" />
                                                </TransformGroup>
                                            </Border.RenderTransform>
                                            <Border HorizontalAlignment="Stretch" Opacity="1.0" Padding="1" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3">
                                                <Border.RenderTransform>
                                                    <TransformGroup>
                                                        <ScaleTransform />
                                                        <SkewTransform />
                                                        <RotateTransform />
                                                        <TranslateTransform X="-2" Y="-2" />
                                                    </TransformGroup>
                                                </Border.RenderTransform>
                                                <Border.Background>
                                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                        <GradientStop Color="#FFDDDDDD" Offset="0"/>
                                                        <GradientStop Color="#AADDDDDD" Offset="1"/>
                                                    </LinearGradientBrush>
                                                </Border.Background>
                                                <samples:DataGridSelectionAdapter 
                                                x:Name="SelectionAdapter" 
                                                AutoGenerateColumns="False"
                                                IsReadOnly="True">
                                                    <samples:DataGridSelectionAdapter.Columns>
                                                        <data:DataGridTextColumn Header="" FontWeight="Bold" Foreground="#CC000000" Binding="{Binding CodeFaa}" />
                                                        <data:DataGridTextColumn Header="Airport" Binding="{Binding LimitedName}" />
                                                        <data:DataGridTextColumn Header="City" Binding="{Binding City}" />
                                                        <data:DataGridTextColumn Header="State" Binding="{Binding State}" />
                                                    </samples:DataGridSelectionAdapter.Columns>
                                                </samples:DataGridSelectionAdapter>
                                            </Border>
                                        </Border>
                                    </Border>
                                </Popup>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
            <ContentControl Content="AutoCompleteBox with a DataGrid DropDown" Style="{StaticResource Header}" />

        <StackPanel>
            
            <TextBlock Style="{StaticResource Information}" UseLayoutRounding="False">
                This sample uses a custom ISelectionAdapter that derives from the 
                Silverlight DataGrid.
            </TextBlock>
            
            <controls:AutoCompleteBox
                x:Name="MyAutoCompleteBox"
                Style="{StaticResource DataGridAutoCompleteTemplate}" 
                />
        </StackPanel>
        
        <src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="DataGridAutoCompleteBox.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl x:Class="Microsoft.Windows.Controls.Samples.DataGridAutoCompleteBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"&gt;
    &lt;StackPanel&gt;
        &lt;StackPanel.Resources&gt;
            &lt;Style x:Key="DataGridAutoCompleteTemplate" TargetType="controls:AutoCompleteBox"&gt;
                &lt;Setter Property="Background" Value="#FF1F3B53"/&gt;
                &lt;Setter Property="IsTabStop" Value="False" /&gt;
                &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt;
                &lt;Setter Property="BorderBrush"&gt;
                    &lt;Setter.Value&gt;
                        &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                            &lt;GradientStop Color="#FFA3AEB9" Offset="0"/&gt;
                            &lt;GradientStop Color="#FF8399A9" Offset="0.375"/&gt;
                            &lt;GradientStop Color="#FF718597" Offset="0.375"/&gt;
                            &lt;GradientStop Color="#FF617584" Offset="1"/&gt;
                        &lt;/LinearGradientBrush&gt;
                    &lt;/Setter.Value&gt;
                &lt;/Setter&gt;
                &lt;Setter Property="Template"&gt;
                    &lt;Setter.Value&gt;
                        &lt;ControlTemplate TargetType="controls:AutoCompleteBox"&gt;
                            &lt;Grid Margin="{TemplateBinding Padding}"&gt;
                                &lt;VisualStateManager.VisualStateGroups&gt;
                                    &lt;VisualStateGroup x:Name="PopupStates"&gt;
                                        &lt;VisualStateGroup.Transitions&gt;
                                            &lt;VisualTransition GeneratedDuration="0:0:0.2" To="PopupOpened" /&gt;
                                            &lt;VisualTransition GeneratedDuration="0:0:0.5" To="PopupClosed" /&gt;
                                        &lt;/VisualStateGroup.Transitions&gt;
                                        &lt;VisualState x:Name="PopupOpened"&gt;
                                            &lt;Storyboard&gt;
                                                &lt;DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="1.0" /&gt;
                                            &lt;/Storyboard&gt;
                                        &lt;/VisualState&gt;
                                        &lt;VisualState x:Name="PopupClosed"&gt;
                                            &lt;Storyboard&gt;
                                                &lt;DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="0.0" /&gt;
                                            &lt;/Storyboard&gt;
                                        &lt;/VisualState&gt;
                                    &lt;/VisualStateGroup&gt;
                                &lt;/VisualStateManager.VisualStateGroups&gt;
                                &lt;TextBox IsTabStop="True" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" Margin="0" /&gt;
                                &lt;Popup x:Name="Popup"&gt;
                                    &lt;Border x:Name="PopupBorder" HorizontalAlignment="Stretch" Opacity="0.0" BorderThickness="0" CornerRadius="3"&gt;
                                        &lt;Border.RenderTransform&gt;
                                            &lt;TranslateTransform X="2" Y="2" /&gt;
                                        &lt;/Border.RenderTransform&gt;
                                        &lt;Border.Background&gt;
                                            &lt;SolidColorBrush Color="#11000000" /&gt;
                                        &lt;/Border.Background&gt;
                                        &lt;Border HorizontalAlignment="Stretch" BorderThickness="0" CornerRadius="3"&gt;
                                            &lt;Border.Background&gt;
                                                &lt;SolidColorBrush Color="#11000000" /&gt;
                                            &lt;/Border.Background&gt;
                                            &lt;Border.RenderTransform&gt;
                                                &lt;TransformGroup&gt;
                                                    &lt;ScaleTransform /&gt;
                                                    &lt;SkewTransform /&gt;
                                                    &lt;RotateTransform /&gt;
                                                    &lt;TranslateTransform X="-1" Y="-1" /&gt;
                                                &lt;/TransformGroup&gt;
                                            &lt;/Border.RenderTransform&gt;
                                            &lt;Border HorizontalAlignment="Stretch" Opacity="1.0" Padding="1" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3"&gt;
                                                &lt;Border.RenderTransform&gt;
                                                    &lt;TransformGroup&gt;
                                                        &lt;ScaleTransform /&gt;
                                                        &lt;SkewTransform /&gt;
                                                        &lt;RotateTransform /&gt;
                                                        &lt;TranslateTransform X="-2" Y="-2" /&gt;
                                                    &lt;/TransformGroup&gt;
                                                &lt;/Border.RenderTransform&gt;
                                                &lt;Border.Background&gt;
                                                    &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                                        &lt;GradientStop Color="#FFDDDDDD" Offset="0"/&gt;
                                                        &lt;GradientStop Color="#AADDDDDD" Offset="1"/&gt;
                                                    &lt;/LinearGradientBrush&gt;
                                                &lt;/Border.Background&gt;
                                                &lt;samples:DataGridSelectionAdapter 
                                                x:Name="SelectionAdapter" 
                                                AutoGenerateColumns="False"
                                                IsReadOnly="True"&gt;
                                                    &lt;samples:DataGridSelectionAdapter.Columns&gt;
                                                        &lt;data:DataGridTextColumn Header="" FontWeight="Bold" Foreground="#CC000000" Binding="{Binding CodeFaa}" /&gt;
                                                        &lt;data:DataGridTextColumn Header="Airport" Binding="{Binding LimitedName}" /&gt;
                                                        &lt;data:DataGridTextColumn Header="City" Binding="{Binding City}" /&gt;
                                                        &lt;data:DataGridTextColumn Header="State" Binding="{Binding State}" /&gt;
                                                    &lt;/samples:DataGridSelectionAdapter.Columns&gt;
                                                &lt;/samples:DataGridSelectionAdapter&gt;
                                            &lt;/Border&gt;
                                        &lt;/Border&gt;
                                    &lt;/Border&gt;
                                &lt;/Popup&gt;
                            &lt;/Grid&gt;
                        &lt;/ControlTemplate&gt;
                    &lt;/Setter.Value&gt;
                &lt;/Setter&gt;
            &lt;/Style&gt;
        &lt;/StackPanel.Resources&gt;
            &lt;ContentControl Content="AutoCompleteBox with a DataGrid DropDown" Style="{StaticResource Header}" /&gt;

        &lt;StackPanel&gt;
            
            &lt;TextBlock Style="{StaticResource Information}" UseLayoutRounding="False"&gt;
                This sample uses a custom ISelectionAdapter that derives from the 
                Silverlight DataGrid.
            &lt;/TextBlock&gt;
            
            &lt;controls:AutoCompleteBox
                x:Name="MyAutoCompleteBox"
                Style="{StaticResource DataGridAutoCompleteTemplate}" 
                /&gt;
        &lt;/StackPanel&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="DataGridAutoCompleteBox.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// A sample AutoCompleteBox with a DataGrid selection adapter.
    /// &lt;/summary&gt;
    [Sample("DataGrid", DifficultyLevel.Advanced)]
    [Category("AutoCompleteBox")]
    public partial class DataGridAutoCompleteBox : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the type.
        /// &lt;/summary&gt;
        public DataGridAutoCompleteBox()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        /// &lt;summary&gt;
        /// Handle the loaded event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Bind to the sample airport data
            MyAutoCompleteBox.ItemsSource = Airport.SampleAirports;
            
            // A custom search, the same that is used in the basic lambda file
            MyAutoCompleteBox.SearchMode = AutoCompleteSearchMode.Custom;
            MyAutoCompleteBox.ItemFilter = (search, item) =&gt;
            {
                Airport airport = item as Airport;
                if (airport != null)
                {
                    // Interested in: Name, City, FAA code
                    string filter = search.ToUpper(CultureInfo.InvariantCulture);
                    return (airport.CodeFaa.ToUpper(CultureInfo.InvariantCulture).Contains(filter)
                        || airport.City.ToUpper(CultureInfo.InvariantCulture).Contains(filter)
                        || airport.Name.ToUpper(CultureInfo.InvariantCulture).Contains(filter));
                }

                return false;
            };
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="DataGridSelectionAdapter.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// An implementation of ISelectionAdapter for the DataGrid control. This 
    /// adapter, unlike the standard SelectorSelectionAdapter, actually derives 
    /// directly from DataGrid.
    /// &lt;/summary&gt;
    public class DataGridSelectionAdapter : DataGrid, ISelectionAdapter
    {
        /// &lt;summary&gt;
        /// Gets or sets a value indicating whether the selection should be 
        /// ignored. Since the DataGrid automatically selects the first row 
        /// whenever the data changes, this simple implementation only works 
        /// with key navigation and mouse clicks. This prevents the text box 
        /// of the AutoCompleteBox control from being updated continuously.
        /// &lt;/summary&gt;
        private bool IgnoreAnySelection { get; set; }

        /// &lt;summary&gt;
        /// Gets or sets a value indicating whether the selection change event 
        /// should not be fired.
        /// &lt;/summary&gt;
        private bool IgnoringSelectionChanged { get; set; }

        /// &lt;summary&gt;
        /// Occurs when the currently selected item changes.
        /// &lt;/summary&gt;
        public new event SelectionChangedEventHandler SelectionChanged;

        /// &lt;summary&gt;
        /// An event that indicates that a selection is complete and has been 
        /// made, effectively a commit action.
        /// &lt;/summary&gt;
        public event RoutedEventHandler Commit;

        /// &lt;summary&gt;
        /// An event that indicates that the selection operation has been 
        /// canceled.
        /// &lt;/summary&gt;
        public event RoutedEventHandler Cancel;

        /// &lt;summary&gt;
        /// Initializes a new instance of the SelectorSelectionAdapter class.
        /// &lt;/summary&gt;
        public DataGridSelectionAdapter()
        {
            base.SelectionChanged += OnSelectionChanged;
            MouseLeftButtonUp += OnSelectorMouseLeftButtonUp;
        }

        /// &lt;summary&gt;
        /// Gets or sets the selected item through the adapter.
        /// &lt;/summary&gt;
        public new object SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }

            set
            {
                IgnoringSelectionChanged = true;
                base.SelectedItem = value;
                IgnoringSelectionChanged = false;
            }
        }

        /// &lt;summary&gt;
        /// Handles the mouse left button up event on the selector control.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnSelectorMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IgnoreAnySelection = false;

            OnSelectionChanged(this, null);
            OnCommit(this, new RoutedEventArgs());
        }

        /// &lt;summary&gt;
        /// Handles the SelectionChanged event on the Selector control.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The selection changed event data.&lt;/param&gt;
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IgnoringSelectionChanged)
            {
                return;
            }

            if (IgnoreAnySelection)
            {
                return;
            }

            SelectionChangedEventHandler handler = this.SelectionChanged;
            if (handler != null)
            {
                handler(sender, e);
            }
        }

        /// &lt;summary&gt;
        /// Gets or sets the items source.
        /// &lt;/summary&gt;
        public new IEnumerable ItemsSource
        {
            get { return base.ItemsSource; }
            
            set
            {
                if (base.ItemsSource != null)
                {
                    INotifyCollectionChanged notify = base.ItemsSource as INotifyCollectionChanged;
                    if (notify != null)
                    {
                        notify.CollectionChanged -= OnCollectionChanged;
                    }
                }

                base.ItemsSource = value;

                if (base.ItemsSource != null)
                {
                    INotifyCollectionChanged notify = base.ItemsSource as INotifyCollectionChanged;
                    if (notify != null)
                    {
                        notify.CollectionChanged += OnCollectionChanged;
                    }
                }
            }
        }

        /// &lt;summary&gt;
        /// Handles the CollectionChanged event, resetting the selection 
        /// ignore flag.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            IgnoreAnySelection = true;
        }

        /// &lt;summary&gt;
        /// Gets the observable collection set by AutoCompleteBox.
        /// &lt;/summary&gt;
        private ObservableCollection&lt;object&gt; Items
        {
            get { return ItemsSource as ObservableCollection&lt;object&gt;; }
        }

        /// &lt;summary&gt;
        /// Increment the selected index, or wrap.
        /// &lt;/summary&gt;
        private void SelectedIndexIncrement()
        {
            SelectedIndex = SelectedIndex + 1 &gt;= Items.Count ? -1 : SelectedIndex + 1;
            ScrollIntoView(SelectedItem, this.Columns[0]);
        }

        /// &lt;summary&gt;
        /// Decrement the SelectedIndex, or wrap around, inside the nested 
        /// SelectionAdapter's control.
        /// &lt;/summary&gt;
        private void SelectedIndexDecrement()
        {
            int index = SelectedIndex;
            if (index &gt;= 0)
            {
                SelectedIndex--;
            }
            else if (index == -1)
            {
                SelectedIndex = Items.Count - 1;
            }

            ScrollIntoView(SelectedItem, this.Columns[0]);
        }

        /// &lt;summary&gt;
        /// Process a key down event.
        /// &lt;/summary&gt;
        /// &lt;param name="e"&gt;The key event arguments object.&lt;/param&gt;
        public void HandleKeyDown(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    OnCommit(this, e);
                    e.Handled = true;
                    break;

                case Key.Up:
                    IgnoreAnySelection = false; 
                    SelectedIndexDecrement();
                    e.Handled = true;
                    break;

                case Key.Down:
                    if ((ModifierKeys.Alt &amp; Keyboard.Modifiers) == ModifierKeys.None)
                    {
                        IgnoreAnySelection = false;
                        SelectedIndexIncrement();
                        e.Handled = true;
                    }
                    break;

                case Key.Escape:
                    OnCancel(this, e);
                    e.Handled = true;
                    break;

                default:
                    break;
            }
        }

        /// &lt;summary&gt;
        /// Fires the Commit event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnCommit(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler handler = Commit;
            if (handler != null)
            {
                handler(sender, e);
            }

            AfterAdapterAction();
        }

        /// &lt;summary&gt;
        /// Fires the Cancel event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnCancel(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler handler = Cancel;
            if (handler != null)
            {
                handler(sender, e);
            }

            AfterAdapterAction();
        }

        /// &lt;summary&gt;
        /// Change the selection after the actions are complete.
        /// &lt;/summary&gt;
        private void AfterAdapterAction()
        {
            IgnoringSelectionChanged = true;
            SelectedItem = null;
            SelectedIndex = -1;
            IgnoringSelectionChanged = false;

            // Reset, to ignore any future changes
            IgnoreAnySelection = true;
        }

        /// &lt;summary&gt;
        /// Initializes a new instance of a DataGridAutomationPeer.
        /// &lt;/summary&gt;
        /// &lt;returns&gt;Returns a new DataGridAutomationPeer.&lt;/returns&gt;
        public AutomationPeer CreateAutomationPeer()
        {
            return new DataGridAutomationPeer(this);
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>
